package fragment.detialfragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import bean.RecordBean;
import qf.wlh.com.oneprisdent.PlayVideoActivity;
import qf.wlh.com.oneprisdent.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.RetrofitRecordFragmentInterface;

import static android.content.ContentValues.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecordDetialFragment extends Fragment {

    private View view;
    private ImageView reImageView1;
    private ImageView reImageView2;
    private TextView reTitle;
    private TextView reContent;
    private ImageView rePlayImageView;
    private TextView rePlayCounts;
    private TextView reLikeCounts;
    private CheckBox downRadioButton;
    private CheckBox reRestorage;
    private CheckBox reShare;
    private CheckBox reLike;

    private int id;
    private String name;

    public RecordDetialFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_record_detial, container, false);
        Bundle bundle = getArguments();
        id = bundle.getInt("id");

        initView();

        Retrofit retrofit =  new  Retrofit.Builder()
                .baseUrl("http://api.yixi.tv/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitRecordFragmentInterface retrofitRecordFragmentInterface = retrofit.create(RetrofitRecordFragmentInterface.class);
        retrofitRecordFragmentInterface.getData().enqueue(new Callback<RecordBean>() {
            private String path;

            @Override
            public void onResponse(Call<RecordBean> call, Response<RecordBean> response) {
                RecordBean body = response.body();
                RecordBean.DataBean dataBean = body.getData().get(id);
                //给detialfragment 也就是viewpager上面的额detialfragment赋值
                if (dataBean.getBackground() != null){
                    Picasso.with(getContext()).load(dataBean.getBackground()).skipMemoryCache().into(reImageView1);
                }
                if (dataBean.getCover() != null){
                    Picasso.with(getContext()).load(dataBean.getCover()).skipMemoryCache().into(reImageView2);
                }
                if (dataBean.getTitle() != null){
                    reTitle.setText(dataBean.getTitle());
                }
                if (dataBean.getDesc() != null){
                    reContent.setText(dataBean.getDesc());
                }
                if (dataBean.getViewnum() != null){
                    rePlayCounts.setText("播放"+dataBean.getViewnum()+"次");
                }
                if (dataBean.getLikenum() != null){
                    reLikeCounts.setText(dataBean.getLikenum()+"人点赞");
                }
                name = dataBean.getVideo();
                Log.e(TAG, "ReconrdDetialFragmegnt: "+dataBean.getVideo());
            }

            @Override
            public void onFailure(Call<RecordBean> call, Throwable t) {

            }
        });

        rePlayImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PlayVideoActivity.class);
                intent.putExtra("name",name);

                startActivity(intent);
            }
        });
        return view;
    }

    private void initView() {
        reImageView1 = ((ImageView) view.findViewById(R.id.record_fragment_detial_imageview1));
        reImageView2 = ((ImageView) view.findViewById(R.id.record_fragment_detial_imageview2));
        reTitle = ((TextView) view.findViewById(R.id.record_fragment_detial_title));
        reContent = ((TextView) view.findViewById(R.id.record_fragment_detial_content));
        rePlayImageView = ((ImageView) view.findViewById(R.id.record_fragment_detial_play));

        rePlayCounts = ((TextView) view.findViewById(R.id.record_fragment_detial_playcount));
        reLikeCounts = ((TextView) view.findViewById(R.id.record_fragment_detial_likecount));

        downRadioButton = ((CheckBox) view.findViewById(R.id.record_fragment_detial_down));
        reRestorage = ((CheckBox) view.findViewById(R.id.record_fragment_detial_restorge));
        reShare = ((CheckBox) view.findViewById(R.id.record_fragment_detial_share));
        reLike = ((CheckBox) view.findViewById(R.id.record_fragment_detial_like));
    }

}
